package gui.actions;

import gui.GuiManager;

import common.Fictionator;
import common.interfaces.ConfigurationsInterface;
import common.interfaces.PlayInterface;

public class QuitAction extends WorkerAction {

    private static final long serialVersionUID = 1L;

    @Override
    public boolean isAllowToRunOperation() {
        return true;
    }
    
    @Override
    public void runOperation() throws Exception {
        ConfigurationsInterface config = Fictionator.getInstance().getConfigurationsLogic();
        PlayInterface playLogic = Fictionator.getInstance().getPlayLogic();
        try {
            playLogic.finishGame();
            int guiSessionID = Fictionator.getInstance().getGuiSessionID();
            config.cleanGUISession(guiSessionID);
        } finally {
            config.closeResources();
        }
    }
    
    @Override
    public void operationSucceed() {
        quitApplication();
    }
    
    @Override
    public void operationFailed(Exception e) {
        quitApplication();
    }
    
    private void quitApplication() {
        GuiManager.disposeAndExit();
    }
    
    public void quit() {
        actionPerformed(null);
    }
}
